<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\home\controller;

use app\home\BaseController;
use think\facade\View;
use think\facade\Db;


class Index extends BaseController
{
    public function index()
    {
        add_user_log('view', '首页');

        $uid=get_login_user('id');
        $check=$this->check_checkin($uid);

        View::assign('is_signed', $check);  
        return View();
    }
    //执行签到程序
	public function do_signin()
    {
 
      $user = $this->checkLogin();
      $check=$this->check_checkin($user['id']);
      if('true'==$check){
        $msg='对不起，今日已签到，请明天再来！';
        return to_assign (1,$msg); 
      }

      $data['uid']=get_login_user('id');
      $data['create_time']=time();
      
      $ip = app('request')->ip();
      $pc='T'.substr($ip,-2);

      // 将时间戳转换为日期格式
      $data['desc']='用户:'.$user['name'].'在'.date('Y-m-d H:i:s',$data['create_time']).'成功签到！签到机器：'.$pc;
      if($pc!=$user['login_pc']){
      // 增加的积分值
      $scoreToAdd = 5;
      $data['desc'].='[异常]';
      $data['status']=-1;

      }else{
      // 增加的积分值
      $scoreToAdd = 10;
      $data['status']=1;

      }

      Db::name('User')->where('id', $user['id'])->inc('score3', $scoreToAdd)->inc('score',$scoreToAdd)->update();      
      $res=Db::name('Checkin')->save($data);

        if ($res) {
            $msg=$user['name'].": 恭喜您，签到成功！积分增加".$scoreToAdd."。";
            
            $param=['score'=>$scoreToAdd,'type'=>'add','content'=>'签到'];
            add_score_log('签到成功',$user['id'], $scoreToAdd, $param);            
            return to_assign (0,$msg);           
               
        } else {
            $msg='服务器错误，签到失败！';
           return to_assign (1,$msg);
        }

      
    }
    public function logs()
    {
        add_user_log('view', '开发日志');
        $seo = get_system_config('web');
        $seo['title'] = '开发日志—勾股博客';
        View::assign('seo',$seo);
        return View('');
    }

	public function down()
    {
        $version = CMS_VERSION;
        add_user_log('down', $version.'版本代码');
        header("Location: https://www.gougucms.com/storage/gougucms_v".$version."_full.zip");
        //确保重定向后，后续代码不会被执行
        exit;
    }
}
